// Contains the API. That means the functions asked by the teacher.
// Must not contain more than translation of internal functions to
// the asked prototype.

#ifdef SWIG
%module bfs
#define SWIG_FILE_WITH_INIT
#define COMPILE_TO_PYTHON

%rename(formatfs) Minformat;
%rename(closefs) Miniclosefs;
%rename(df) Minidf;
%rename(ls) Minils;
%rename(mkdir) Minimkdir;
%rename(cd) Minicd;
%rename(rm) Minirm;
%rename(open) Miniopen;
%rename(close) Miniclose;
%rename(getc) Minigetc;
%rename(putc) Miniputc;
%rename(seek) Miniseek;
#endif

#if defined _COMPILING_TO_DLL
	#define EXPORT __declspec(dllexport)
#else
	#define EXPORT
#endif

#if defined __DEBUG__ && !defined SWIG
#include "path.h"
HardDriveInterface_t *hdi;
FileSystemHandle_t *fsh;
#endif

// File opening flags. Some are missing, but not needed.
#define FS_RDONLY 0x01
#define FS_WRONLY 0X02
#define FS_RDWR (FS_WRONLY|FS_RDONLY)
#define FS_TRUNC 0x04
#define FS_APPEND (0x08|FS_WRONLY)
#define FS_CREAT 0x10

#ifndef SEEK_SET
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#endif

#ifdef SWIG
//%inline %{
#endif


/// Create the file system, on a blockCount blocks virtual disk.
///
/// \param blockCount The block count on the virtual disk.
/// \param blockSize The block size on the virtual disk.
/// \return 0 if successful.
EXPORT int Minformat(unsigned short int blockSize,unsigned int blockCount);

/// Close the file system, and delete the hdi.
///
/// The non-Miniformat functions cannot be used
/// before Miniformat is called again.
/// \return -1 if the deletion failed, 0 otherwise.
EXPORT int Miniclosefs(void);

/// Prints to stdout some data about the disk space left.
/// \return 0 if succeded.
EXPORT int Minidf(void);

/// List file content on stdout.
///
/// Formating is similar to ls -l.
/// \return 0 if successful
EXPORT int Minils(void);

/// See reference to posix mkdir.
///
/// Exceptions :
/// \li \c errno is not set.
/// \li Attempting to set rights will cause undefined behaviour.
EXPORT int Minimkdir(const char* name);

/// See reference to posix chdir.
///
/// Exceptions :
/// \li \c errno is not set.
EXPORT int Minicd(const char *path);

/// See reference to posix unlink.
///
/// Exceptions :
/// \li \c errno is not set.
/// \li Fails if someone have an desciptor on it opened.
/// \li Also removes directories, and their content recursively.
EXPORT int Minirm(const char* itemName);

/// See reference to posix open.
///
/// Exceptions :
/// \li \c errno is not set.
/// \li Only these flags are supported : FS_RDONLY,FS_WRONLY,FS_RDWR,FS_TRUNC,
/// FS_APPEND,FS_CREAT. Their meaning is the same as if they started by O_***.
/// \li FS_APPEND only place the cursor at the file at the file opening, not on every write.
EXPORT int Miniopen(char *name,int flags);

/// See reference to posix close.
///
/// Exceptions :
/// \li \c errno is not set.
/// \li Only closes file descriptors. Not unexpected.
EXPORT int Miniclose(int handle);

/// See reference to posix lseek.
///
/// The flags are the same as the posix one.
/// Exceptions :
/// \li \c errno is not set.
EXPORT int Miniseek(int fildes, char offset, int whence);

#ifndef SWIG

/// See reference to posix read.
///
/// Exceptions :
/// \li \c errno is not set.
/// \li The function fails if a read is attempted beyond the file end, even
/// if it started before.
/// \todo Solve the "all or nothing" problem on partial reads.
EXPORT int Miniread(int fildes, void *buf, unsigned int  nbyte);

/// See reference to posix write.
///
/// Exceptions :
/// \li \c errno is not set.
/// \li The function fails if a read is attempted beyond the file end, even
/// if it started before.
/// \todo Solve the "all or nothing" problem on partial reads.
EXPORT int Miniwrite(int fildes, const void *buf, unsigned int nbyte);

#endif

EXPORT int Minicp(const char *from,const char *to);

/// See documentation of putc in iso C standard.
int Miniputc(int fd, char c);

/// See documentation of getc in iso C standard.
int Minigetc(int fd);

#ifdef SWIG

//%}

#endif
